//index.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js')

Page({
  data: {
    isLogin: false,
    userInfo: {},
    hasUserInfo: false,
    selectAccountIndex: 0,
    accountArray: [
      
    ],
    orders:[
    ]
  },
  setDataPro:function(obj){
    this.setData(obj);
  }
  ,onLoad: function () {
    wx.hideTabBar({
      aniamtion: false
    })
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true,
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  onReady: function () {
    // Do something when page ready.
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const setData = this.setDataPro;
    const currentData = util.formatTime(new Date());
    const openId = app.globalData.openId;
    if(openId){
      wx.request({
        url: app.globalData.baseUrl +'order/queryCurrentDay?date='+currentData+"&openId="+openId,
        method: "GET",
        success: function (res) {
          if(res.data.code==0){
            console.log(res.data);
            const orderLists=new Object;
            orderLists.orders=res.data.data;
            setData(orderLists);
          }
        }
      });
    }
  },
  navigateToOrderShow:function(event){
    const orderType = event.currentTarget.dataset.type;
    const itemData = event.currentTarget.dataset.item;
    const params = JSON.stringify(itemData);
    if(orderType==0){
      wx.navigateTo({
        url: '/pages/transportOrderShow/transportOrderShow?order='+params,
      })
    }else{
      wx.navigateTo({
        url: '/pages/repairOrderShow/repairOrderShow?order='+params,
      })
    }
  }
  ,navigateToMonthOrders:function(evnet){
    wx.navigateTo({
      url: '/pages/monthOrders/monthOrders',
    })
  }
  ,navigateToAllOrders:function(event){
    wx.navigateTo({
      url: '/pages/allOrders/allOrders',
    })
  }
  ,toggleLoginStatus: function () {
    this.setData({
      isLogin: true
    });
  }
  ,login:function(){
    const setData = this.setDataPro;
    const currentData = util.formatTime(new Date());
    wx.request({
      url: app.globalData.baseUrl +'account/login', //仅为示例，并非真实的接口地址
      data: app.globalData.jdCode,
      method: "POST",
      success: function (res) {
        if (res.data.code == 0) {
          const account = res.data.data;
          if (account) {
            const openId = account.openId;
            app.globalData.openId = openId;
            const dataObj = new Object;
            dataObj.openId = openId;
            dataObj.isLogin = true;
            dataObj.account = account.realName;
            setData(dataObj);
            const data = new Object;
            data.account = account;
            wx.setStorageSync(openId, account);
            wx.showTabBar({
              aniamtion:false
            });
            if (account.role =='normal'){
                var accounts=[];
                accounts.push(account);
                const obj = new Object;
                obj.accountArray = accounts;
                setData(obj);
            }else{
              wx.request({
                url: app.globalData.baseUrl + 'account/queryAllAccounts',
                method: "GET",
                success: function (res) {
                  if (res.data.code == 0) {
                    console.log(res.data);
                    const obj = new Object;
                    obj.accountArray = res.data.data;
                    setData(obj);
                  }
                }
              });
            }
            //登录成功之后,查询用户今日订单
            wx.request({
              url: app.globalData.baseUrl + 'order/queryCurrentDay?date=' + currentData + "&openId=" + openId,
              method: "GET",
              success: function (res) {
                if (res.data.code == 0) {
                  console.log(res.data);
                  const orderLists = new Object;
                  orderLists.orders = res.data.data;
                  setData(orderLists);
                }
              }
            });
          }
        } else {
          console.log(res.data);
        }
      }
    });
  }
  ,toRegister:function(){
    wx.navigateTo({
      url: '/pages/register/register',
    })
  }
})
